//
//  WJ_Me_Search_VC.m
//  WJBSApp
//
//  Created by dllo on 16/7/16.
//  Copyright © 2016年 王景伟. All rights reserved.
//
// 搜索栏  搜索本地
#import "WJ_Me_Search_VC.h"
#import "WJMeModel.h"
#import "WJMeSearchCell.h"
#import "WJ_Search_XiangQingVC.h"
#import "WJDataManager.h"

@interface WJ_Me_Search_VC ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain) UISearchBar *searchBar;

@property (nonatomic,retain) WJBaseTableView *searchTableView;
// 有想要的数据
@property (nonatomic,retain) NSMutableArray *searchArray;

@end

@implementation WJ_Me_Search_VC

static NSString *const  searchCell = @"searchCell";

-(void)dealloc
{
    [_searchBar release];
    [_searchTableView release];
    [_searchArray release];
    self.searchBar.delegate = nil;
    self.searchTableView.delegate = nil;
    self.searchTableView.dataSource = nil;
    [super dealloc];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.searchArray = [NSMutableArray array];
    
    [self createConfig];
    
    [self createView];
}

- (void)createConfig
{
    // 影藏系统的返回按钮
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationButtonReturn@3x副本.png" hightImage:@"navigationButtonReturnClick@3x副本.png" target:self action:@selector(SearchLeftAction)];
}

#pragma Mark- 进入上一个界面
- (void)SearchLeftAction
{
    [self.searchBar resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
}




- (void)createView
{
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 20, WIDTH - 100, 44)];
    self.searchBar.placeholder = @"搜索您想要订阅的";
    self.navigationItem.titleView = self.searchBar;
    self.searchBar.delegate = self;
    [_searchBar release];
    
    self.searchTableView = [[WJBaseTableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    self.searchTableView.delegate = self;
    self.searchTableView.dataSource = self;
    self.searchTableView.rowHeight = HEIGHT / 9;
    self.searchTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.searchTableView registerClass:[WJMeSearchCell class ] forCellReuseIdentifier:searchCell];
    [self.view addSubview:self.searchTableView];
    [_searchTableView release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.searchArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WJMeSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:searchCell];
    WJMeModel *model = self.searchArray[indexPath.row];
    cell.model = model;
    
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:model.theme_name];
    //  _searchText = [_searchText stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:_searchText]];
    NSRange rang = [model.theme_name rangeOfString:self.searchBar.text];
    //设置标签文字属性
    [attributeString setAttributes:[NSMutableDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, nil] range:rang];
    
    cell.theme_nameLable.attributedText = attributeString;
    
    return cell;
}

#pragma mark - 点击进入推荐详情页面  点击方法页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    WJ_Search_XiangQingVC *vc = [[WJ_Search_XiangQingVC alloc]init];
    vc.model = self.searchArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
    
}

// 显示搜索按钮 并且设置它的状态
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont systemFontOfSize:15];
    attr[NSForegroundColorAttributeName] = [UIColor redColor];
    [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:@"取消"];
    [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:attr forState:UIControlStateNormal];
    [searchBar setShowsCancelButton:YES animated:YES];
    return YES;
}

#pragma mark -  点击取消按钮 实现的方法 进入上一个界面
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 实现键盘上Search按钮的方法  谓词...
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    if (searchBar.text.length !=0 ) {
        //清空搜索数组
        [self.searchArray removeAllObjects];

        for (WJMeModel *model in [[WJDataManager shareFMDB]selectAllSec]) {
            
            NSString* str = searchBar.text;
            
            // 是否包含那两个字  创建条件@""里面是条件语句
            NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c]%@ ",str];
            
            // 下面的代码就是剩下的代码
            if ([namePredicate evaluateWithObject:model.theme_name]) {
                [self.searchArray addObject:model];
                NSLog(@"%@",model.theme_name);
            }
            
        }
        [self.searchTableView reloadData];
        
     }
}

#pragma mark - 实现监听输入完毕的方法
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    if (searchBar.text.length !=0 ) {
        
        for (WJMeModel *model in [[WJDataManager shareFMDB]selectAllSec]) {
            
            NSString* str = searchBar.text;
            
            // 是否包含那两个字  创建条件
            NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c]%@ ",str];
            //清空搜索数组
            [self.searchArray removeAllObjects];
            
            // 下面的代码就是剩下的代码
            if ([namePredicate evaluateWithObject:model.theme_name]) {
                [self.searchArray addObject:model];
                NSLog(@"1");
            }
            
        }
        [self.searchTableView reloadData];
        
    }
    return YES;
}




@end
